package common.util;

/**
 * @(#) Configuration.java
 * Copyright 2001 by  Hyundai, Inc.,  All rights reserved.
 * 
 * NOTICE !      You can copy or redistribute this code freely, 
 * but you should not remove the information about the copyright notice 
 * and the author.
 * 
 * @author  WonYoung Lee, javaservice@hanmail.net
 */
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;


public class Configuration
{
	private static String path = "";
	private static long last_modified = 0;
	private static Properties props = null;

	private static String file_name = null;
	static {
		//File  default_file = new File(System.getProperty("user.home"), "common.properties");
		File  default_file = new File("D:\\Projects\\new_elamp", "common.properties");
		file_name = System.getProperty("common.config", default_file.getAbsolutePath());
	}

	/**
	 *
	 */
	public Configuration() throws ConfigurationException {
		super();
		initialize();
	}

	/**
	 * 
	 */
	protected void initialize() throws ConfigurationException {
		synchronized(Configuration.class){	
			try{
				boolean needUpdate = false;

				File file = new File(file_name);
				if ( ! file.canRead() ) 
					throw new ConfigurationException( this.getClass().getName() + " - Can't open configuration file: " + file_name );
				
				if ( (last_modified != file.lastModified() ) || props == null ) {
					needUpdate = true;
				}

				if ( needUpdate ) {
					props = new java.util.Properties();
					FileInputStream fin = new FileInputStream(file);
					props.load(new java.io.BufferedInputStream(fin));
					fin.close();
					last_modified = file.lastModified();
				} // end if
			}
			catch(ConfigurationException e) {
				last_modified = 0;
				throw e;
			}
			catch(Exception e){
				last_modified = 0;
				throw new ConfigurationException( this.getClass().getName() + " - Can't load configuration file: " + e.getMessage());
			}
		} // end of sunchronized(lock);
	}

	/**
	 * @return java.lang.String
	 * @param key java.lang.String
	 */
	public String get(String key) {
		return getString(key);
	}
	/**
	 * 
	 * @return boolean
	 * @param key java.lang.String
	 */
	public boolean getBoolean(String key) {
		boolean value = false;
		try {
			value = (new Boolean(props.getProperty(key))).booleanValue();
		}
		catch(Exception e){
			throw new IllegalArgumentException("Illegal Boolean Key : " + key);
		}
		return value;
	}

	/**
	 * @return int
	 * @param key java.lang.String
	 */
	public int getInt(String key) {
		int value = -1;
		try {
			value = Integer.parseInt(props.getProperty(key));
		}
		catch(Exception e){
			throw new IllegalArgumentException("Illegal Integer Key : " + key);
		}
		return value;
	}

	/**
	 * @return int
	 * @param key java.lang.String
	 */
	public long getLong(String key) {
		long value = -1;
		try {
			value = Long.parseLong(props.getProperty(key));
		}
		catch(Exception e){
			throw new IllegalArgumentException("Illegal Integer Key : " + key);
		}
		return value;
	}
	/**
	 * @return java.util.Properties
	 */
	public java.util.Properties getProperties() {
		return props;
	}

	/**
	 * @return java.lang.String
	 * @param key java.lang.String
	 */
	public String getString(String key) {
		String value = null;
		try {
			String tmp = props.getProperty(key);
			if ( tmp == null ) throw new Exception("value of key(" +key+") is null" );
			value = CommonUtil.toKorean(tmp);
		}
		catch(Exception e){
			throw new IllegalArgumentException("Illegal String Key : " + key);
		}
		return value;
	}
}
